package com.heima.oss;

/**
 * @Author XHui
 * @Since 2024/2/6 16:03
 * @Version 1.0
 */
public class ThreadLocalDemo {

    public static void main(String[] args) throws InterruptedException {

        ThreadLocal<Integer> intLocal = new ThreadLocal<Integer>(){
            @Override
            protected Integer initialValue() {
                return 0;
            }
        };
        intLocal.set(1);
        new Thread(
                () -> {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    intLocal.set(intLocal.get() + 1);
                    intLocal.remove();
                    System.out.println("intLocal.get() = " + intLocal.get());
                    System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
                    System.out.println("值1 = " + intLocal.get());
        }).start();

        Thread.sleep(1000);
        System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
        System.out.println("值2 = " + intLocal.get());
    }
}
